package com.maxlen.order.config;

import com.maxlen.common.rabbitMQConstant.CouponRabbitMQConstant;
import com.maxlen.common.rabbitMQConstant.OrderRabbitMQConstant;
import com.maxlen.common.rabbitMQConstant.RabbitMQConfigConstant;
import com.maxlen.common.rabbitMQConstant.WareRabbitMQConstant;
import com.maxlen.order.entity.OrderEntity;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.handler.annotation.Headers;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@Configuration
public class MyMQConfig {

    @Bean
    public Exchange orderEventExchange(){
        return new TopicExchange(OrderRabbitMQConstant.ORDER_EVENT_EXCHANGE, true, false, null);
    }

    @Bean
    public Queue orderOrderDelayQueue(){
        Map<String, Object> map = new HashMap<>();
        map.put(RabbitMQConfigConstant.X_DEAD_LETTER_EXCHANGE, OrderRabbitMQConstant.ORDER_EVENT_EXCHANGE);
        map.put(RabbitMQConfigConstant.X_DEAD_LETTER_ROUTING_KEY, OrderRabbitMQConstant.ORDER_ORDER_RELEASE_BINDING);
        map.put(RabbitMQConfigConstant.X_MESSAGE_TTL, 10000);
        return new Queue(OrderRabbitMQConstant.ORDER_ORDER_DELAY_QUEUE, true, false, false, map);
    }

    @Bean
    public Queue orderOrderReleaseQueue(){
        return new Queue(OrderRabbitMQConstant.ORDER_ORDER_RELEASE_QUEUE, true, false, false);
    }

    @Bean
    public Queue orderFinishUserQueue(){
        return new Queue(OrderRabbitMQConstant.ORDER_FINISH_USER_QUEUE, true, false, false);
    }

    @Bean
    public Queue orderFinishWareQueue(){
        return new Queue(OrderRabbitMQConstant.ORDER_FINISH_WARE_QUEUE, true, false, false);
    }

    @Bean
    public Binding orderOrderDelayBinding(){
        return new Binding(
                OrderRabbitMQConstant.ORDER_ORDER_DELAY_QUEUE,
                Binding.DestinationType.QUEUE,
                OrderRabbitMQConstant.ORDER_EVENT_EXCHANGE,
                OrderRabbitMQConstant.ORDER_ORDER_DELAY_BINDING,
                null);
    }

    @Bean
    public Binding orderOrderReleaseBinding(){
        return new Binding(
                OrderRabbitMQConstant.ORDER_ORDER_RELEASE_QUEUE,
                Binding.DestinationType.QUEUE,
                OrderRabbitMQConstant.ORDER_EVENT_EXCHANGE,
                OrderRabbitMQConstant.ORDER_ORDER_RELEASE_BINDING,
                null);
    }

    @Bean
    public Binding orderFinishUserBinding(){
        return new Binding(
                OrderRabbitMQConstant.ORDER_FINISH_USER_QUEUE,
                Binding.DestinationType.QUEUE,
                OrderRabbitMQConstant.ORDER_EVENT_EXCHANGE,
                OrderRabbitMQConstant.ORDER_FINISH,
                null);
    }

    @Bean
    public Binding orderFinishWareBinding(){
        return new Binding(
                OrderRabbitMQConstant.ORDER_FINISH_WARE_QUEUE,
                Binding.DestinationType.QUEUE,
                OrderRabbitMQConstant.ORDER_EVENT_EXCHANGE,
                OrderRabbitMQConstant.ORDER_FINISH,
                null);
    }

    @Bean
    public Binding orderCouponReleaseOtherBinding(){
        return new Binding(
                CouponRabbitMQConstant.COUPON_COUPON_RELEASE_QUEUE,
                Binding.DestinationType.QUEUE,
                OrderRabbitMQConstant.ORDER_EVENT_EXCHANGE,
                OrderRabbitMQConstant.ORDER_RELEASE_OTHER,
                null);
    }

    @Bean
    public Binding orderWareStockReleaseOtherBinding(){
        return new Binding(
                WareRabbitMQConstant.WARE_STOCK_RELEASE_QUEUE,
                Binding.DestinationType.QUEUE,
                OrderRabbitMQConstant.ORDER_EVENT_EXCHANGE,
                OrderRabbitMQConstant.ORDER_RELEASE_OTHER,
                null);
    }

}
